package cn.signalpha.modules.base.controller.wx;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.tool.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
import org.springframework.web.bind.annotation.*;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_BASE_NAME + "/admin/wx/menu")
@Api(value = "微信菜单", tags = "微信菜单")
public class WxMenuController {

    private final WxMpService wxMpService;

	@GetMapping("/list")
	@ApiOperation(value = "获取菜单列表", notes = "")
	public R<WxMpGetSelfMenuInfoResult> getSelfMenuInfo() throws WxErrorException {
		return R.data(wxMpService.getMenuService().getSelfMenuInfo());
	}

    @PostMapping("/create")
	@ApiOperation(value = "创建菜单", notes = "")
    public R menuCreate(@RequestBody WxMenu menu) throws WxErrorException {
		String appId = wxMpService.getWxMpConfigStorage().getAppId();
		menu.getButtons().forEach(wxMenuButton -> {
			wxMenuButton.setAppId(appId);
			wxMenuButton.getSubButtons().forEach(wxMenuButton1 -> {
				wxMenuButton1.setAppId(appId);
			});
		});
		return R.data(wxMpService.getMenuService().menuCreate(menu));
    }

}
